//
// HistogramDialog.java
//
package com.photoexpert;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import javax.swing.JPanel;

/*
 *  Histogram calculation is based on the NTSC definition on luminance
 *  luminance = 0.299 * R + 0.587 * G + 0.114 * B
 */

class HistogramDialog extends javax.swing.JDialog {

    HistogramDialog() {
        super(App.Instance(), true);
        initComponents();
        setLocation(200, 200);
        // display
        BufferedImage img = Implementor.Instance().getImage();
        int graph[] = new int[766];
        int width = img.getWidth();
        int height = img.getHeight();
        int highValue = 0;
        for(int w = 0; w < width; ++w)
            for(int h = 0; h < height; ++h) {
                int pixel = img.getRGB(w, h);
                int r = (pixel & 0x00ff0000) >> 16;
                int g = (pixel & 0x0000ff00) >> 8;
                int b = pixel & 0x000000ff;
                int luminance = (int)(0.299 * r + 0.587 * g + 0.114 * b);
                graph[luminance]++;
                if(graph[luminance] > highValue)
                    highValue = graph[luminance];
            }
        App._logger.log(Level.INFO, "HighValue={0}", highValue);
        for(int i = 0; i < 256; ++i)
            _data[i] = graph[i] * 0.7f / highValue ;
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _histogram = new Histogram();
        _cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Histogram");
        setModal(true);

        _histogram.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout _histogramLayout = new javax.swing.GroupLayout(_histogram);
        _histogram.setLayout(_histogramLayout);
        _histogramLayout.setHorizontalGroup(
            _histogramLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 374, Short.MAX_VALUE)
        );
        _histogramLayout.setVerticalGroup(
            _histogramLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 178, Short.MAX_VALUE)
        );

        _cancel.setText("Cancel");
        _cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(_cancel)
                    .addComponent(_histogram, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(_histogram, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelActionPerformed
        setVisible(false);
    }//GEN-LAST:event__cancelActionPerformed

    private class Histogram extends JPanel {
        @Override
        public void paint(Graphics g) {
            int width = getWidth() - 2;
            int height = getHeight();
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, width + 1, height - 1);
            for(int w = 0; w < width; ++w) {
                int w1 = (int)(w * 1f / width * 256);
                if(_data[w1] >= 1f)
                    g.drawLine(w, 1, w, height - 1);
                else {
                    int h = (int)(_data[w1] * height);
                    if(h > 0)
                        g.drawLine(w, height - h, w, height - 1);
                }
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _cancel;
    private javax.swing.JPanel _histogram;
    // End of variables declaration//GEN-END:variables
    private float                   _data[] = new float[256];

}
